package com.edu.dao;

import com.edu.entity.Food;
import com.edu.entity.FoodType;
import com.edu.utils.JdbcHelper;
import com.edu.utils.PageList;
import com.edu.utils.PageParam;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author ZCN
 * @Date 2021/11/12 16:31
 * @Version 1.0
 **/
public class FoodDao {

    // 分页数据
    public PageList pageList(PageParam pageParam) throws SQLException {
        String countsql = "select count(*) from food";
        String pagesql ="select * from food limit ?,?";
        Long count = (Long) JdbcHelper.getSingle(countsql);
        List data = JdbcHelper.query(pagesql, Food.class, pageParam.getStart(),pageParam.getLimit());
        return new PageList(count,data);
    }
    // 获取热门菜品
    public List<Object> getHotsFood() throws SQLException {
        String sql = "select * from food order by hits desc";
        List<Object> hotsfoods = JdbcHelper.query(sql, Food.class);
        return hotsfoods;
    }

    // 获取厨师推荐
    public List<Object> getcookRecomment() throws SQLException{
        String sql = "select * from food where comment = 0";
        List<Object> cookRecommentfoods = JdbcHelper.query(sql, Food.class);
        return cookRecommentfoods;
    }



    // 查询所有
    public List<Food> findAll() throws SQLException {
        String sql = "select * from food";
        List<Food> list = JdbcHelper.query(sql,Food.class);
        return list;
    }

    // 根据id查询
    public Food findOne(Integer id) throws SQLException {
        String sql = "select * from food where id = ?";
        List<Food> list = JdbcHelper.query(sql, Food.class, id);
        return list.get(0);
    }

    // 修改
    public void update(Food food) throws SQLException {
        String sql = "update food set " +
                "foodname=? , feature=? ," +
                "material=? ,price=? ," +
                "type=? ,picture=? ," +
                "comment=? ," +
                "typename=?,flag=?" +
                " where id=?";
        JdbcHelper.update(sql, food.getFoodname(),food.getFeature(),
                                food.getMaterial(),food.getPrice(),
                                food.getType(),food.getPicture(),
                                food.getComment(),
                                food.getTypename(),food.getFlag(),
                food.getId());
    }

    // 上架
    public void delete(Integer id) throws SQLException {
        String sql = "update food set flag = 0 where id = ?";
        JdbcHelper.update(sql,id);
    }

    // 上架
    public void online(Integer id) throws SQLException {
        String sql = "update food set flag = 1 where id = ?";
        JdbcHelper.update(sql,id);
    }

    // 添加
    public void insert(Food food) throws SQLException {
        String sql = "insert into food(" +
                "foodname," +
                "feature," +
                "material," +
                "price," +
                "type," +
                "picture," +
                "hits," +
                "comment," +
                "typename," +
                "flag"+
                ") " +
                "values (?,?,?,?,?,?,?,?,?,?)";
        JdbcHelper.update(sql,food.getFoodname(),food.getFeature(),food.getMaterial(),
                food.getPrice(),food.getType(),food.getPicture(),0,food.getComment(),
                food.getTypename(),1);
    }
}
